package com.yf.modules.system.controller;

import com.yf.Utils.PageUtil;
import com.yf.domain.dict.Dict;
import com.yf.model.query.DictQueryCriteria;
import com.yf.modules.system.service.api.DictService;
import com.yf.response.ResponseBody;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * @author chentianwei
 * @date 2024-10-14 13:42
 * @description 字典模块Restful
 */
@RestController
@RequestMapping("/api/dict")
@Slf4j
@Tag(name = "DictApi", description = "字典Api")
public class DictController {

    private final DictService dictService;

    public DictController(DictService dictService) {
        this.dictService = dictService;
    }

    @Operation(summary = "查询字典", security = @SecurityRequirement(name = "authScheme"))
    @GetMapping(value = "")
    public ResponseEntity<Object> queryAllDict() {
        List<Dict> dict = dictService.queryDict(new DictQueryCriteria());
        return ResponseEntity.ok().body(ResponseBody.success(PageUtil.toPage(dict, dict.size())));
    }

    @Operation(summary = "添加新字典", security = @SecurityRequirement(name = "authScheme"))
    @PostMapping("/add")
    public ResponseEntity<ResponseBody<Object>> addDict(@RequestBody Dict dict) {
        dictService.create(dict);
        return ResponseEntity.ok(ResponseBody.success("添加成功"));
    }

    @Operation(summary = "修改字典", security = @SecurityRequirement(name = "authScheme"))
    @PutMapping("/edit")
    public ResponseEntity<ResponseBody<Object>> editDict(@RequestBody Dict dict) {
        dictService.edit(dict);
        return ResponseEntity.ok(ResponseBody.success("修改成功"));
    }

    @Operation(summary = "删除字典", security = @SecurityRequirement(name = "authScheme"))
    @DeleteMapping("/delete")
    public ResponseEntity<ResponseBody<Object>> editDict(@RequestBody Set<Long> ids) {
        dictService.delete(ids);
        return ResponseEntity.ok(ResponseBody.success("删除成功"));
    }

    @Operation(summary = "导出字典数据", security = @SecurityRequirement(name = "authScheme"))
    @GetMapping(value = "/download")
    public void exportDict(HttpServletResponse response) throws IOException {
        dictService.download(response);
    }


}
